﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using ChatDLuXService;

namespace ChatDLuXService.Model
{
    [Serializable]
    [DataContract (IsReference=true)]
    public class User
    {
        // Instantiate Utilities object to use methods
        Utilities util = new Utilities();

        //TODO Finish class
        private long _pkID;
        private string _username;
        private string _password;
        private string _email;
        private bool _isActive;
        private bool _isAdmin;
        private IBroadcastorCallBack _callback;

        [DataMember]
        public long PkId
        {
            get { return _pkID; }
            set { _pkID = value; }
        }
        
        [DataMember]
        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        [DataMember]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        [DataMember]
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        [DataMember]
        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }

        [DataMember]
        public bool IsAdmin
        {
            get { return _isAdmin; }
            set { _isAdmin = value; }
        }

        [DataMember]
        public IBroadcastorCallBack callback
        {
            get { return _callback; }
            set { _callback = value; }
        }


        // Login service calls this to create a user object
        public User(string username, string password)
        {
            // Get credentials from db
            DataTable dt = util.GetLoginInfo(username, password);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow r in dt.Rows)
                {
                    PkId = Convert.ToInt64(r["pkID"].ToString());
                    Username = r["Username"].ToString();
                    //Password = r["Password"].ToString();
                    Email = r["Email"].ToString();
                    IsActive = Boolean.Parse(r["isActive"].ToString());
                    IsAdmin = Boolean.Parse(r["isAdmin"].ToString());
                }
            }
        }


    }
}